package 
{
	import com.adobe.utils.AGALMiniAssembler;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.Stage3D;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DTextureFormat;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.Program3D;
	import flash.display3D.textures.Texture;
	import flash.display3D.VertexBuffer3D;
	import flash.events.Event;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;

	
	[SWF(width = '465',height = '465', frameRate= '60')]
public class SimpleStage2D extends Sprite
{
	private var stage3D:Stage3D;
	private	var bd2:BitmapData;
	private	var context3D:Context3D;
	

	private var vertBak:Vector.<Number>;
	private var vertexbuffer:VertexBuffer3D;
	private var indexbuffer:IndexBuffer3D;
	private var program:Program3D;
	private	var texture:Texture;
	private	var bp:Bitmap;
	private	var count:int = 0;
	
	[Embed(source = "bg.png")]private var bg:Class;
	
	public function SimpleStage2D()
	{
		stage3D = this.stage.stage3Ds[0];
		//stage3D.x = 50;
		//stage3D.y = 50;
		bd2 = new bg().bitmapData;
		
		stage3D.addEventListener("context3DCreate",initMolehill);
		stage3D.requestContext3D();
	}
	
	private function initMolehill(event:Event):void
	{
		context3D = stage3D.context3D;

		// 1
		context3D.configureBackBuffer(400, 400, 4, false);
		
		// 2
		var vertexShaderAssembler : AGALMiniAssembler = new AGALMiniAssembler();
		vertexShaderAssembler.assemble( Context3DProgramType.VERTEX,
			"m44 op, va0, vc0\n" + 
			"mov v0, va1" 
		);
		var fragmentShaderAssembler : AGALMiniAssembler= new AGALMiniAssembler();
		fragmentShaderAssembler.assemble( Context3DProgramType.FRAGMENT,
			"tex ft1, v0, fs0 <2d, linear ,nomip> \n" +
			"mov oc, ft1"
		);
		
		program = context3D.createProgram();
		program.upload( vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode);  
		
		// 3
		texture = context3D.createTexture(bd2.width,bd2.height,Context3DTextureFormat.BGRA,true);
		texture.uploadFromBitmapData(bd2);

		// 4
		vertBak  = vertices.slice(0);
		vertexbuffer = context3D.createVertexBuffer(4,5);
		vertexbuffer.uploadFromVector(vertices, 0, 4);
		
		// 5
		var indices:Vector.<uint >  = Vector.<uint > ([0,1,2,1,2,3]);
		indexbuffer = context3D.createIndexBuffer(6);
		indexbuffer.uploadFromVector(indices,0,6);

		this.addEventListener(Event.ENTER_FRAME,updateRender);
	}

	private	var tw:Number = 128 / 400;
	private	var th:Number = 256 / 400;
	private var m_matrix:Matrix3D = new Matrix3D();
	private var vertices: Vector.<Number> = Vector.<Number>(
	
	[
	
	//-1,        1 - 2*th,  0, 0, 1, // 左下
	//-1,        1,         0, 0, 0, // 左上
	//-1 + 2*tw, 1 - 2*th,  0, 1, 1, // 右下
	//-1 + 2 * tw, 1,         0, 1, 0   // 右下
	
	-tw,    -th,         0, 0, 1, // 左下
	-tw,     th,         0, 0, 0, // 左上
	tw,     -th,         0, 1, 1, // 右下
	tw,      th         ,0, 1, 0   // 右下
	]);
	
	
															
	private function updateRender(event:Event):void
	{
		if (++count>130)
		{
			count = 0;
			vertices = vertBak.slice(0);
		}
		
		// 1
		context3D.clear( 1,0, 0, 0 );
		
		// 2
		
		//m_matrix.appendScale(0.01,0.01,1)
		//m_matrix.appendTranslation(1 - tw, 1 - th, 0);
		m_matrix.appendRotation(1, Vector3D.Z_AXIS);
		m_matrix.appendTranslation(1 / 400, -1 / 400, 0);

		//m_matrix.appendScale(1.01, 1.01, 1);
		//m_matrix.appendTranslation(tw - 1, th -1, 0);
		
		context3D.setProgram(program);
		context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, m_matrix, true);
		//m.appendRotation(40, Vector3D.Z_AXIS);
		// 3
		context3D.setTextureAt(0, texture);
		
		// 4
		//for (var i:int = 0; i <4; i++)
		//{

			//vertices[i * 5 + 3] -= 0.0008;

			//vertices[i * 5 + 4] -= 0.0006;
		//};
		vertexbuffer.uploadFromVector(vertices, 0,4);
		context3D.setVertexBufferAt(0, vertexbuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
		context3D.setVertexBufferAt(1, vertexbuffer, 3, Context3DVertexBufferFormat.FLOAT_2);
		
		// 5
		context3D.drawTriangles(indexbuffer);
		
		// 6
		context3D.present();
	}
	
}
	
}